<?php
class Temp{
    static private $var=array();
    static private $prefix="<{";//后缀
    static private $endfix="}>";//前缀
    static private $cache_path=CACHE_PATH;//缓存路径
    //把变量赋值到模板上
    static public function assign($_var,$value){
        Temp::$var[$_var]=$value;
    }
//加载模板，规则安装templates目录下对应的控制器名+方法名=目录+文件名
    static public function display($path=''){
        //把变量导出到模板上
        $action=array_keys($_REQUEST,0);
        $action=(isset($action[0])?$action[0]:"");
        $jp_arg=  explode("/", $action);
        $a= (isset($jp_arg[1])?$jp_arg[1]:"");
        $c=(isset($jp_arg[0])?$jp_arg[0]:"");
        extract(Temp::$var);
        $jp_arg["action"]=$action;
        $jp_arg["c"]=$c;
        $jp_arg["a"]=$a;
        foreach ($_REQUEST as $k=>$v){
            if($v){
                $jp_arg[$k]=$v;
            }
        }
        global $_G;
        $temppath=ROOT."templates/tpl/";
       
        if($path!=''){
            //如果带斜杠,就可能去搜其他控制器目录下的模板
            if(strpos($path,"/")){
               $file_path=$temppath.strtolower($path).".htm";
                if(file_exists($file_path)){
                    $file_path=self::SaveCacheFile($file_path);
                    include $file_path; 
                }else {
                    Debugshow::show("模板文件不存在！");
                }
            }else{
                $file_path=$temppath.strtolower($_G["web"]["controll"])."/".strtolower($path).".htm";
                if(file_exists($file_path)){
                    $file_path=self::SaveCacheFile($file_path);
                    include $file_path; 
                }else {
                    Debugshow::show("模板文件不存在！");
                }

                  }
        }else{
            //模板文件是否存在
          $file_path=$temppath.strtolower($_G["web"]["controll"])."/".strtolower($_G["web"]["action"]).".htm";
          if(file_exists($file_path)){
              $file_path=self::SaveCacheFile($file_path);
              include $file_path; 
          }else {
              Debugshow::show("模板文件不存在！");
          }
        }
       
}
static private function SaveCacheFile($sourcepath){
    $fc=new Filecontrol();
    $fc->mkdirs(self::$cache_path);
    $destfilepath=self::$cache_path.md5($sourcepath).".htm";
    if(!file_exists($destfilepath)){
    $content=  file_get_contents($sourcepath);
    $content=self::replace_content($content);
    file_put_contents($destfilepath, $content);
    }else{
        $edittime=filemtime($destfilepath);
        if(time()-$edittime>CACHE_TIMEOUT){
            $content=  file_get_contents($sourcepath);
            $content=  self::replace_content($content);
            file_put_contents($destfilepath, $content);
        }
    }
   
    
    return $destfilepath;
    
}
//模板引擎替换规则
static private function replace_content($content){
     $prefix_first=self::$prefix;
     $endfix_end=self::$endfix;
     $content=  preg_replace('/Tinclude\s*?("[A-Za-z0-9_\/]*?\")/i', 'Temp::Tinclude($1);', $content);
     $content=  preg_replace('/'.$prefix_first.'\s*if\s*(.*?)\s*'.$endfix_end.'/i', $prefix_first.' if($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*if\s*\((.*?)\)\s*'.$endfix_end.'/i', $prefix_first.' if($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*elseif\s*(.*?)\s*'.$endfix_end.'/i', $prefix_first.' }elseif($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*elseif\s*\((.*?)\)\s*'.$endfix_end.'/i', $prefix_first.'}elseif($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*else\s*?'.$endfix_end.'/i', $prefix_first.'}else{'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*\/if\s*?'.$endfix_end.'/i', $prefix_first.'}'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*foreach\s*(.*?)\s*'.$endfix_end.'/i', $prefix_first.' foreach($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*foreach\s*\((.*?)\)\s*'.$endfix_end.'/i', $prefix_first.' foreach($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*for\s+(.*?)\s*'.$endfix_end.'/i', $prefix_first.' for($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*for\s+\((.*?)\)\s*'.$endfix_end.'/i', $prefix_first.' for($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*while\s*[\(\s](.*?)[\)\s]?\s*'.$endfix_end.'/i',$prefix_first.' while($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*switch\s*[\(\s](.*?)[\)\s]?\s*'.$endfix_end.'/i', $prefix_first.' switch($1){'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*case\s*[\(\s](.*?)[\)\s]?\s*'.$endfix_end.'/i', $prefix_first.' case $1:'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*default\s*'.$endfix_end.'/i', $prefix_first.' default:'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*\$([A-Za-z0-9_\"\[\]\-\>]+)\s*'.$endfix_end.'/i', $prefix_first.' echo $$1; '.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*\/foreach\s*?'.$endfix_end.'/i', $prefix_first.'}'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*\/while\s*?'.$endfix_end.'/i',$prefix_first.'}'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*\/switch\s*?'.$endfix_end.'/i', $prefix_first.'}'.$endfix_end, $content);
     $content=  preg_replace('/'.$prefix_first.'\s*\/for\s*?'.$endfix_end.'/i', $prefix_first.'}'.$endfix_end, $content);
     $content=str_replace(self::$prefix, "<?php ", $content);
     $content=str_replace(self::$endfix, " ?>", $content);
     return $content;
}

//引入模板文件，参数：模板文件路径
static public function Tinclude($path=''){
    $action=array_keys($_REQUEST,0);
        $action=(isset($action[0])?$action[0]:"");
        $jp_arg=  explode("/", $action);
        $a= (isset($jp_arg[1])?$jp_arg[1]:"");
        $c=(isset($jp_arg[0])?$jp_arg[0]:"");
     extract(Temp::$var);
     $jp_arg["action"]=$action;
        $jp_arg["c"]=$c;
        $jp_arg["a"]=$a;
     foreach ($_REQUEST as $k=>$v){
            if($v){
                $jp_arg[$k]=$v;
            }
        }
     $temppath=ROOT."templates/tpl/";
     if($path!=''){
         $file_path=$temppath.strtolower($path).".htm";
         if(file_exists($file_path)){
              $file_path=self::SaveCacheFile($file_path);
              include $file_path; 
         }else
         {
             Debugshow::show("模板文件不存在！");
         }
     }else
     {
         Debugshow::show("模板文件不存在！");
     }
}
}
